package com.cloud.zk;

import lombok.SneakyThrows;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ZkController {
    @SneakyThrows
    @GetMapping("/node")
    public void testNode() {
        ZookeeperClient zookeeperClient = new ZookeeperClient();
        String ROOT_PATH = "/myapp";
        String nodePath = ROOT_PATH + "/node1";

//        zookeeperClient.addWatch(nodePath);
        // 创建节点
        zookeeperClient.createEphemeralNode(nodePath, "Hello, Zookeeper!");
        // 获取节点数据
        String data = zookeeperClient.getData(nodePath);
        System.out.println("获取到的节点数据: " + data);
        // 设置节点数据
        zookeeperClient.setData(nodePath, "Updated data");
        // 获取更新后的数据
        data = zookeeperClient.getData(nodePath);
        System.out.println("获取到的更新后数据: " + data);
        // 判断节点是否存在
        boolean exists = zookeeperClient.exists(nodePath);
        System.out.println("节点是否存在: " + exists);
        // 删除节点
        zookeeperClient.deleteNode(nodePath);
        // 判断节点是否存在
        exists = zookeeperClient.exists(nodePath);
        System.out.println("节点是否存在（删除后）: " + exists);
        // 关闭 Zookeeper 客户端
//        zookeeperClient.close();
    }
}
